// 相关流程数据
const Mock = require('mockjs')
const notice = Mock.mock({
  // 流程信息
  "processData|3": [{
    id:"@increment(100000,999999)",  //id
    processId: "process_@increment(100000,999999)", //流程编号
    'processName|1': ["请假申请", "调岗申请"],//流程名称
    processName: "", //流程名称
    crateDate: "@date(yyyy-MM-dd)", //创建时间
    // 'processType|1': ["财务", "人力", "业务"], //流程分类
    // version: 0,
    creator: "@cname()", //创建者
    'status|1': ['1', '2'], //状态 
    describe: null,  //流程描述   
    nodeData:{},  // 流程设计数据
  }]
})
module.exports = [
  // 流程信息
  {
    url: '/vue-element-admin/processNew/list',
    type: 'post',
    response: config => {
      // const body = config.body
      const {
        processId,
        processName,
        status,
        processType,
        pagenum,
        pagesize,
        order,
      } = config.body.query
      var searchDataList = []
      var searchKeyList = []

      if (status === '0') {
        searchDataList = notice.processData
      } else {
        for (let i = 0; i < notice.processData.length; i++) {
          if (notice.processData[i].status === status) {
            searchDataList.push(notice.processData[i]);
          }
        }
      }

      //多条件查询
      searchKeyList = searchDataList.filter(item => {
        let flag = true
        if (processId && item.processId.indexOf(processId) === -1) {
          flag = false
        }
        if (processName && item.processName.indexOf(processName) === -1) {
          flag = false
        }
        if (processType && item.processType.indexOf(processType) === -1) {
          flag = false
        }
        return flag
      })
      if (order == 2) {
        searchKeyList.sort((a, b) => {
          return new Date(b.crateDate) - new Date(a.crateDate);
        });
      } else {
        searchKeyList.sort((a, b) => {
          return new Date(a.crateDate) - new Date(b.crateDate);
        });
      }

      // 数据整理
      var [index, size, total] = [pagenum, pagesize, searchKeyList.length]
      var len = total / size
      var totalPages = len - parseInt(len) > 0 ? parseInt(len) + 1 : len
      var newDataList = searchKeyList.slice((index - 1) * size, index * size)


      return {
        code: 20000,
        data: {
          pagenum: index,
          pagesize: size,
          content: newDataList,
          total: total,
          totalPages: totalPages,

        }
      }
    }
  },
  // 获取单个流程数据
  {
    url: '/vue-element-admin/processNew/getData',
    type: 'post',
    response: config => {
      const {
       Id
      } = config.body
      for (const process of notice.processData) {
        if (process.processId === Id) {
          return {
            code: 20000,
            data: process
          }
        }
      }
      
    }
  },
// 编辑
// 编辑数据
{
    url: '/vue-element-admin/processNew/editData',
    type: 'post',
    response: config => {
      console.log(config.body)
      const body = config.body
      var index = notice.processData.findIndex((item) => {
        return item.processId === body.processId
      });
      notice.processData.splice(index, 1, Mock.mock({
        id:body.id,
        processId: body.processId,
        processName: body.processName,
        processType: body.processType,
        describe: body.describe,
        crateDate:body.crateDate,
        creator:body.creator,
        status:body.status,
        nodeData:body.nodeData,
        formConf:body.formConf,
        tableType:body.tableType
      }))
      return {
        code: 20000,
        msg: "编辑成功",
        data: {
          content: notice.processData,
          total: notice.processData.length,
        }
      }
    }
},
// 新增
{
    url: '/vue-element-admin/processNew/addData',
    type: 'post',
    response: config => {
      console.log(config.body)
      const body = config.body
      notice.processData.push(
        Mock.mock({
          id:"@increment(100000,999999)",
          processId: body.processId,
          processName: body.processName,
          processType: body.processType,
          describe: body.describe,
          crateDate:body.crateDate,
          creator:body.creator,
          status:body.status,
          nodeData:body.nodeData,
          formConf:body.formConf,
          tableType:body.tableType
        })
      )
      return {
        code: 20000,
        msg: "添加成功",
        data: {
          content: notice.processData,
        }
      }
    }
  },

]
